// pages/netCase/evidenceInfo/evidenceInfo.js
import ecfsConfig from '../../../config/ecfs.config.js'
import { opinionKinds } from '../../../js/netCase/evidence/evidenceData.js'
import locale from '../../../config/locale/zh-CN/netCase/evidence.js'
import * as userApi from '../../../api/user.js'
import * as evidenceApi from '../../../api/evidence.js'
Page({

  /**
   * 
   * 页面的初始数据
   */
  data: {
    locale,
    isDisabled: false,
    evidenceId: '',
    certificateNumber: '',
    opinionKinds,
    opinionKindsValue: ['对证据的真实性存在异议', '对证据的合法性存在异议', '对证据的关联性存在异议'],
    caseId: '',
    evidenceExplain: '',
    evidenceFiles: [],
    evidenceName: '',
    id: '',
    partyId: '',
    partyName: '',
    partyNameList: [{
      id: '1111',
      name: '嘻嘻'
    },
    {
      id: '111111111111',
      name: '哈哈'
    },
    {
      id: '22',
      name: '啦啦'
    },
    {
      id: '33',
      name: '丫丫'
    }],
    source: '',
    index: -1,
    isFromEdit: true,
    crossExaminations: [],
    isShowAddOpinion: false,
    tempFilePathList: [],
  },
  evidenceName: function (e) {
    this.setData({
      evidenceName: e.detail.value,
    })
  },
  evidenceExplain: function (e) {
    this.setData({
      evidenceExplain: e.detail.value,
    })
  },
  pickerPartyName: function (e) {
    Object.keys(this.data.partyNameList).forEach(key => {
      if (e.detail.value === key) {
        this.setData({
          partyId: this.data.partyNameList[key].id,
          partyName: this.data.partyNameList[key].name,
          index: e.detail.value
        })
      }
    })
  },
  addOpinion() {
    wx.navigateTo({
      url: '../opinion/opinion?evidenceid=' + this.data.evidenceId,
    })
  },
  uploadFiles: function (e) {
    let that = this
    if (this.data.tempFilePathList.length < 5) {
      wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          var tempFilePaths = res.tempFilePaths[0]
          wx.uploadFile({
            url: ecfsConfig.IS_PRODUCTION ? (ecfsConfig.SERVER_DOMAIN + ecfsConfig.UPLOAD_FILE_URL) : (ecfsConfig.SERVER_DOMAIN + ecfsConfig.RAP_URL_PREFIX + ecfsConfig.UPLOAD_FILE_URL),
            header: {
              'Auth-Token': wx.getStorageSync('accessToken')
            },
            filePath: tempFilePaths,
            name: 'file',
            success: function (res) {
              let data = JSON.parse(res.data)
              //   copyEvidenceFiles = that.data.evidenceFiles,
              //   evidenceFileValue = {
              //     fileExtension: data.body[0].extension,
              //     fileSize: data.body[0].fileSize,
              //     newName: data.body[0].fileName,
              //     oldName: data.body[0].originalFileName
              //   }
              // copyEvidenceFiles.push(evidenceFileValue)
              // that.setData({
              //   evidenceFiles: copyEvidenceFiles
              // })
              // console.log(that.data.evidenceFiles)
              //do something
              let copyTempFilePathList = that.data.tempFilePathList
              copyTempFilePathList.push(tempFilePaths)
              that.setData({
                tempFilePathList: copyTempFilePathList
              })
              console.log(that.data.tempFilePathList)
              if (that.data.tempFilePathList.length === 5) {
                that.setData({
                  isFromEdit: false
                })
              }
            }
          })
        }

      })
    } else {
      wx.showToast({
        title: '最多上传5张图片',
        icon: 'none',
        duration: 1000
      })
    }
  },
  prewFile(e) {
    // let path = e.currentTarget.dataset.path
    // wx.previewImage({
    //   current: path, // 当前显示图片的http链接
    //   urls: this.data.tempFilePathList // 需要预览的图片http链接列表
    // })
  },
  submit() {
    if (this.data.evidenceName != '') {
      if (this.data.index != -1) {
        if (this.data.evidenceFiles.length > 0) {
          const config = {
            url: ecfsConfig.IS_PRODUCTION ? (ecfsConfig.SERVER_DOMAIN + ecfsConfig.ADD_EVIDENCE_URL) : (ecfsConfig.SERVER_DOMAIN + ecfsConfig.RAP_URL_PREFIX + ecfsConfig.ADD_EVIDENCE_URL),
            data: {
              caseId: this.data.caseId,
              evidenceExplain: this.data.evidenceExplain,
              evidenceFiles: this.data.evidenceFiles,
              evidenceName: this.data.evidenceName,
              id: this.data.evidenceId,
              partyId: this.data.partyId,
              partyName: this.data.partyName
            },
          }
          evidenceApi.addEvidence(config).then(res => {
            if (res.data.code === 200) {
              wx.navigateTo({
                url: '../evidenceList/evidenceList',
              })
            }
          }).catch(error => {
            console.log(error)
            console.log('is failed')
          })
        } else {
          wx.showToast({
            title: '请上传证据文件',
            icon: 'none',
            duration: 1000
          })
        }
      } else {
        wx.showToast({
          title: '请选择证据所属人',
          icon: 'none',
          duration: 1000
        })
      }
    } else {
      wx.showToast({
        title: '请输入证据名称',
        icon: 'none',
        duration: 1000
      })
    }
  },
  delete() {
    if (this.data.evidenceId != '') {
      const config = {
        url: ecfsConfig.IS_PRODUCTION ? (ecfsConfig.SERVER_DOMAIN + ecfsConfig.DELETE_EVIDENCE_URL) : (ecfsConfig.SERVER_DOMAIN + ecfsConfig.RAP_URL_PREFIX + ecfsConfig.DELETE_EVIDENCE_URL),
        data: {
          caseId: this.data.caseId,
          evidenceId: this.data.evidenceId,
          partyId: this.data.partyId
        },
      }
      evidenceApi.deleteEvidence(config).then(res => {
        if (res.data.code === 200) {
          wx.navigateTo({
            url: '../evidenceList/evidenceList',
          })
        }
      }).catch(error => {
        console.log(error)
        console.log('is failed')
      })
    } else {
      wx.navigateTo({
        url: '../evidenceList/evidenceList',
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      evidenceId: options.evidenceid,
    })
    if (options.evidenceid != undefined) {
      const config = {
        url: ecfsConfig.IS_PRODUCTION ? (ecfsConfig.SERVER_DOMAIN + ecfsConfig.EVIDENCE_INFO_URL) : (ecfsConfig.SERVER_DOMAIN + ecfsConfig.RAP_URL_PREFIX + ecfsConfig.EVIDENCE_INFO_URL),
        data: {
          evidenceId: options.evidenceid,
        },
      }
      evidenceApi.getEvidence(config).then(res => {
        this.setData({
          certificateNumber: res.data.body.certificateNumber,
          evidenceExplain: res.data.body.evidenceExplain,
          evidenceFiles: res.data.body.evidenceFiles,
          evidenceName: res.data.body.evidenceName,
          partyId: res.data.body.partyId,
          partyName: res.data.body.partyName
        })
        Object.keys(this.data.partyNameList).forEach(key => {
          if (this.data.partyId === this.data.partyNameList[key].id) {
            this.setData({
              index: key
            })
          }
        })
      }).catch(error => {
        console.log(error)
        console.log('is failed')
      })

      const opinionConfig = {
        url: ecfsConfig.IS_PRODUCTION ? (ecfsConfig.SERVER_DOMAIN + ecfsConfig.GET_OPINION_URL) : (ecfsConfig.SERVER_DOMAIN + ecfsConfig.RAP_URL_PREFIX + ecfsConfig.GET_OPINION_URL),
        data: {
          evidenceId: options.evidenceid,
        },
      }
      userApi.getUserInfo(opinionConfig).then(res => {
        if (res.data.code === 200) {
          this.setData({
            crossExaminations: res.data.body.crossExaminations,
          })

          const getUserInfoconfig = {
            url: ecfsConfig.IS_PRODUCTION ? (ecfsConfig.SERVER_DOMAIN + ecfsConfig.GET_USERINFO_URL) : (ecfsConfig.SERVER_DOMAIN + ecfsConfig.RAP_URL_PREFIX + ecfsConfig.GET_USERINFO_URL),
          }
          userApi.getUserInfo(getUserInfoconfig).then(res => {
            if (res.data.code === 200) {
              if (this.data.certificateNumber === res.data.body.cardNum) {
                this.setData({
                  isShowAddOpinion: false,
                })
              } else {
                this.setData({
                  isShowAddOpinion: true,
                })
              }
              Object.keys(this.data.crossExaminations).forEach(keyOuter => {
                Object.keys(this.data.opinionKinds).forEach(keyInner => {
                  if (this.data.crossExaminations[keyOuter].evidenceObjection === this.data.opinionKinds[keyInner].status) {
                    this.setData({
                      opinionKindsValue: this.data.opinionKinds[keyInner].kind
                    })
                  }
                })
                if (this.data.crossExaminations[keyOuter].certificateNumber === this.data.certificateNumber) {
                  this.setData({
                    isShowAddOpinion: false,
                  })
                } else {
                  this.setData({
                    isShowAddOpinion: true,
                  })
                }
              })

            }
          }).catch(error => {
            console.log(error)
            console.log('is failed')
          })



        }
      }).catch(error => {
        console.log(error)
        console.log('is failed')
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})